package com.darren.euc.service.impl;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.darren.euc.dao.PlaceDao;
import com.darren.euc.dao.RoomTypeDao;
import com.darren.euc.dao.SpaceDao;
import com.darren.euc.entity.PageBean;
import com.darren.euc.entity.Province;
import com.darren.euc.entity.RoomType;
import com.darren.euc.entity.Space;
import com.darren.euc.entity.SpaceStatus;
import com.darren.euc.entity.User;
import com.darren.euc.service.SpaceService;
import com.darren.euc.util.StringUtil;

import net.sf.json.JSONObject;


@Service
public class SpaceServiceImpl implements SpaceService {
	
	@Autowired
    private SpaceDao spaceDao;
	
	@Autowired
    private RoomTypeDao roomTypeDao;
	
	
	@Autowired
    private PlaceDao placeDao;

    @Override
    public Space save(Space space) {
        return spaceDao.save(space);
    }

    @Override
    public Space update(Space space) {
        return spaceDao.update(space);
    }
    
	@Override
	public void deleteById(Integer id) {
		Space space = new Space();
		space.setId(id);
		spaceDao.delete(space);		
	}
    
	@Override
	public List<Space> getAll() {
		return spaceDao.getAll();
	}
	

	@SuppressWarnings("rawtypes")
	public PageBean queryForPageUseHQL(JSONObject requestJson,Integer pageSize, Integer currentPage){	
		String HQL = null;
		String likes = null;
		String equals = null;
			List<String> parameters = new ArrayList<String>();
		JSONObject likeJson = requestJson.getJSONObject("like");
		Iterator likeIterator = likeJson.keys();
		while(likeIterator.hasNext()){
			String key = (String) likeIterator.next();
			key = StringUtil.convertNullToEmptyString(key);
			String value = likeJson.getString(key);
			value = StringUtil.convertNullToEmptyString(value);
			if(!"".equals(value)){
				likes = (likes == null?"": likes + " OR ") +  key + " like ? ";
				parameters.add("%"+value+"%");
			}
		}
		HQL = likes == null?null:(" (" + likes + ") ");		
		JSONObject equalsJson = requestJson.getJSONObject("equals");
        Iterator equalsIterator = equalsJson.keys();        
		while(equalsIterator.hasNext()){
			String key = (String) equalsIterator.next();
			key = StringUtil.convertNullToEmptyString(key);
			String value = equalsJson.getString(key);
			value = StringUtil.convertNullToEmptyString(value);
			if(!"".equals(value)){
				equals = (equals == null?"": ""+equals + " and ") +  key + " = ? ";
				parameters.add(value);
			}
		}		
		if(HQL != null){
			if(equals != null){
				HQL = HQL + " and " + equals;
			}
		}else{
			HQL = equals;
		}
		HQL = " from Space " + (HQL == null?"":" where " + HQL);
		return spaceDao.queryForPageByHQL(HQL, pageSize, currentPage,parameters.toArray());
	}
	
	@SuppressWarnings("rawtypes")
	public List<Space> queryByCondition(JSONObject requestJson){		
		String HQL = null;
		String likes = null;
		String equals = null;
			List<String> parameters = new ArrayList<String>();
		JSONObject likeJson = requestJson.getJSONObject("like");
		Iterator likeIterator = likeJson.keys();
		while(likeIterator.hasNext()){
			String key = (String) likeIterator.next();
			key = StringUtil.convertNullToEmptyString(key);
			String value = likeJson.getString(key);
			value = StringUtil.convertNullToEmptyString(value);
			if(!"".equals(value)){
				likes = (likes == null?"": likes + " OR ") +  key + " like ? ";
				parameters.add("%"+value+"%");
			}
		}
		HQL = likes == null?null:(" (" + likes + ") ");		
		JSONObject equalsJson = requestJson.getJSONObject("equals");
        Iterator equalsIterator = equalsJson.keys();        
		while(equalsIterator.hasNext()){
			String key = (String) equalsIterator.next();
			key = StringUtil.convertNullToEmptyString(key);
			String value = equalsJson.getString(key);
			value = StringUtil.convertNullToEmptyString(value);
			if(!"".equals(value)){
				equals = (equals == null?"": ""+equals + " and ") +  key + " = ? ";
				parameters.add(value);
			}
		}		
		if(HQL != null){
			if(equals != null){
				HQL = HQL + " and " + equals;
			}
		}else{
			HQL = equals;
		}
		HQL = " from Space " + (HQL == null?"":" where " + HQL);
		return spaceDao.queryByCondition(HQL,parameters.toArray());
	}
	
	
	public List<SpaceStatus> getAllSpaceStatus(){
		return spaceDao.getAllSpaceStatus();
	}
	
	public List<Province> getAllProvinces(){
		return placeDao.getAllProvinces();
	}

	@Override
	public PageBean findPage(Map para, int pn, int ps) {

    	String select = "select * ";
    	String from = "from euc_spaces where status_id=1";

    	if (para.get("city_id") != null)
    		from += " and city_id="+para.get("city_id");
		if (para.get("province_id") != null)
			from += " and province_id="+para.get("province_id");

		PageBean<Space> page = spaceDao.queryForPageBySQL(select + from, ps, pn);

		List list = new ArrayList();

		page.getList().forEach(x->{
			Map map = new HashMap<>();
			map.put("id", x.getId());
			map.put("spaceName", x.getSpaceName());
			map.put("displayPicture", x.getDisplayPicture());
			map.put("address", x.getAddress());
			map.put("detailUrl", "/_space/space_detail?id="+x.getId());
			//map.put("provinceName", x.getProvince().getProvinceName());
			list.add(map);
		});
		page.setList(list);
		return page;
	}

	@Override
	public Map spaceDetail(int id) {
		//配图，名称，描述，地址，电话，类型（人数），设施
		Space space = spaceDao.findById(id);
		Map map = new HashMap();
		map.put("id", space.getId());
		map.put("spaceName", space.getSpaceName());
		map.put("address", space.getAddress());
		map.put("displayPicture", space.getDisplayPicture());
		map.put("displayPicture2", space.getDisplayPicture2());
		map.put("displayPicture3", space.getDisplayPicture3());
		map.put("introduction", space.getIntroduction());
		map.put("contactNumber", space.getContactNumber());
		map.put("seatCount", space.getSeatCount());
		map.put("otherFacilities", space.getOtherFacilities());
		map.put("spaceArea", space.getSpaceArea());
		List<RoomType> roomTypes = roomTypeDao.getAll();
	    map.put("roomTypes", roomTypes);
		return map;
	}

	@Override
	public Space findById(Integer space_id) {
		return spaceDao.findById(space_id);
	}

	@Override
	public Space findByUserId(Integer userId) {
    	String hql = " from Space where createUser.id = ?";
    	List<Space> spaces = spaceDao.queryByCondition(hql,userId);
    	if (spaces != null & spaces.size() > 0){
    		return spaces.get(0);
		}
		return null;
	}


}